import re
import time
import yagmail

from conf import BASE_PATH



def read_data_from_txt(file_path):
    data_dict = {}
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            # 去除每行首尾的空白字符（包括换行符）
            line = line.strip()
            # 分割每行数据，假设每行数据由空格分隔
            key, value = line.split(' ', 1)
            # 将键值对存入字典
            data_dict[key] = int(value)  # 假设值都是整数，故转换为int类型
    return data_dict


def send_email(Case_report,passed,failed_num,skipped,reruns,error_num,duration):
    # 连接邮箱服务器配置
    yag = yagmail.SMTP(user='hfc329422@163.com', password='NEDMWAJGHSHKLTDX', host='smtp.163.com')

    # 读取测试报告内容

    # 文件路径示例
    # file_path = BASE_PATH+'\\report\export\prometheusData.txt'  # 替换为你的文件路径
    # launch_data = read_data_from_txt(file_path)

    # # 打印读取到的数据
    # for key, value in launch_data.items():
    #     print(f" {key}:  {value}")

    current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # 构建邮件内容
    content = (
        "项目名称：云设计UI自动化测试\n"
        f"运行测试用例总数：{Case_report} \n"  # 使用 get 方法时提供默认值，以防键不存在  
        f"通过用例数：{passed} \n"
        f"失败用例数：{failed_num} \n"
        f"跳过用例数：{skipped} \n"
        f"异常用例数：{error_num} \n"
        f"重跑用例数：{reruns} \n"
        f"构建用时：{duration:.2f} 秒 \n"  # 提供默认值 0.0 以避免格式化错误  
        f"构建时间：{current_time}"
    )

    # 发送邮件
    recipient = "3294223160@qq.com"
    subject = "自动化测试报告"
    yag.send(to=recipient, subject=subject, contents=content)
    yag.close()


# if __name__ == '__main__':
#     send_email()